package com.exercise.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.exercise.common.ServiceException;
import com.exercise.entity.Category;
import com.exercise.entity.Dish;
import com.exercise.entity.Employee;
import com.exercise.entity.Setmeal;
import com.exercise.mapper.CategoryMapper;
import com.exercise.mapper.EmployeeMapper;
import com.exercise.service.CategoryService;
import com.exercise.service.DishService;
import com.exercise.service.EmployeeService;
import com.exercise.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 陈凯
 * @create 2023-03-06-11:13
 */
@Service
@Slf4j
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;

    @Override
    public void deleteCategory(Long id) {
        //查询当前分类是否关联菜品，如果关联，则抛出一个业务异常
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId, id);
        int dishCount = dishService.count(dishLambdaQueryWrapper);
        log.info(String.valueOf(dishCount));
        if (dishCount > 0){
            //抛出一个业务异常
            throw new ServiceException("当前分类已经关联了菜品，不能删除");
        }
        //查询当前分类是否关联套餐，如果关联，则抛出一个业务异常
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId, id);
        int setmealCount = setmealService.count(setmealLambdaQueryWrapper);
        log.info(String.valueOf(setmealCount));
        if (setmealCount > 0){
            //抛出一个业务异常
            throw new ServiceException("当前分类已经关联了套餐，不能删除");
        }
        //正常删除分类
        super.removeById(id);
    }
}
